package dry.example.gui;

import javax.swing.*;
import java.awt.*;

public class ResponsiveLayoutDemo {

    public static void main(String[] args) {
        // 创建主窗口
        JFrame frame = new JFrame("响应式布局示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);

        // 设置主面板为 GridBagLayout
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // 添加一个按钮
        JButton button = new JButton("点击我");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0; // 水平拉伸权重
        gbc.weighty = 0.1; // 垂直拉伸权重
        gbc.fill = GridBagConstraints.BOTH; // 拉伸填充
        gbc.insets = new Insets(5, 5, 5, 5); // 内边距
        mainPanel.add(button, gbc);

        // 添加一个表格
        String[] columnNames = {"ID", "姓名", "年龄"};
        Object[][] data = {
                {1, "张三", 28},
                {2, "李四", 32},
                {1, "张三", 28},
                {2, "李四", 32},
                {1, "张三", 28},
                {2, "李四", 32},
                {1, "张三", 28},
                {2, "李四", 32},
                {1, "张三", 28},
                {2, "李四", 32},
                {1, "张三", 28},
                {2, "李四", 32},
                {3, "王五", 25}
        };
        JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);

        gbc.gridy = 1;
        gbc.weighty = 0.9; // 表格占据更多空间
        gbc.fill = GridBagConstraints.BOTH;
        mainPanel.add(scrollPane, gbc);

        // 将主面板加入窗口
        frame.add(mainPanel);
        frame.setVisible(true);
    }

}
